#!/bin/sh
set -e

# first arg is `-f` or `--some-option`
# or first arg is `something.conf`
if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ]; then
	set -- redis-server "$@"
fi

# allow the container to be started with `--user`
if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then
	chown -R redis .
	exec gosu redis "$0" "$@"
fi

# daemonize no or yes
#if [[ "$DEAMONIZE" == "yes" ]] ; then
#    sed -i "s/daemonize yes/daemonize no/g" /usr/local/etc/redis/redis.conf
#fi

# set requirepass
if [ ! -z "$REQUIREPASS" ]; then
    echo "requirepass ${REQUIREPASS}" >> /usr/local/etc/redis/redis.conf
fi

# set port
if [ ! -z "$PORT" ]; then
    sed -i "s/port 6379/port ${PORT}/g" /usr/local/etc/redis/redis.conf
fi

exec "$@"